package com.sqq.config;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.render.ViewType;
import com.sqq.jfinalbase.SqqJFinalConfig;
import com.sqq.jfinalbase.SqqModel;

/**
 * API引导式配置
 */
public class AdminConfig extends SqqJFinalConfig {

    /**
     * 配置常量
     */
    public void configConstant(Constants me) {
        // 加载少量必要配置，随后可用getProperty(...)获取值
        me.setBaseViewPath("/WEB-INF/view");
        FreeMarkerRender.setProperties(loadPropertyFile("freemarker.properties"));
        loadPropertyFile("conf.properties");
        me.setViewType(ViewType.FREE_MARKER);
        me.setDevMode(getPropertyToBoolean("devMode", false));
    }

    /**
     * 配置路由
     */
    public void configRoute(Routes me) {
        me.add(new AutoBindRoutes());
    }

    /**
     * 配置插件
     */
    @SuppressWarnings("unchecked")
    public void configPlugin(Plugins me) {
        // 配置C3p0数据库连接池插件
        C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"),
            getProperty("password").trim());
        me.add(c3p0Plugin);

        // 配置ActiveRecord插件
        AutoTableBindPlugin atbp = new AutoTableBindPlugin(c3p0Plugin);
        atbp.addExcludeClasses(SqqModel.class);
        me.add(atbp);
        
    }

    /**
     * 配置全局拦截器
     */
    public void configInterceptor(Interceptors me) {
    }

    /**
     * 配置处理器
     */
    public void configHandler(Handlers me) {

    }

    @Override
    public void afterJFinalStart() {
        super.afterJFinalStart();
        schedulerinit();//初始化定时任务
    }

    @Override
    public void beforeJFinalStop() {
        if (scheduler != null)
            try {
                scheduler.shutdown(true);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        super.beforeJFinalStop();

    }

    Scheduler scheduler = null;

    public void schedulerinit() {
//        try {
//            scheduler = StdSchedulerFactory.getDefaultScheduler();
//            // define the job and tie it to our HelloJob class
//            JobDetail jobDetail = JobBuilder.newJob(SendWxMessageJob.class)
//                .withIdentity("testJob_1", "group_1").build();
//            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger_1", "group_1")
//                .startNow()
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10) //时间间隔
//                    .repeatForever()).build();
//            // Tell quartz to schedule the job using our trigger
//            scheduler.scheduleJob(jobDetail, trigger);
//            scheduler.start();
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
    }

    /**
     * 建议使用 JFinal 手册推荐的方式启动项目
     * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
     */
    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 80, "/", 5);
    }
}
